'use strict';

const Controller = require('egg').Controller;
const jwt = require('jsonwebtoken')

class HomeController extends Controller {
  // async index() {
  //   const { ctx } = this;
  //   // const {userType} = this.ctx.state;
  //   const userType = 1;
  //   if (userType === 1) {
  //     ctx.body = [
  //       '/user',
  //       '/cart',
  //       '/book'
  //     ]
  //   } else if (userType === 0){
  //     ctx.body = [
  //       '/ab',
  //       '/cd',
  //       '/ef',
  //       '/user',
  //       '/cart',
  //       '/book'
  //     ]
  //   }
  // }
 /**
   * user表
   * 登陆   /api/login  (加密)  post
   * 注册  --postman
   */
  async login() {
    const { username, password } = this.ctx.request.body;
    const data = await this.ctx.app.mysql.get('users', {username, password})
    if (data) {
      const token = jwt.sign({...data}, this.app.config.keys, {
        expiresIn: '24h'
      })
      this.ctx.body = {
        token,
        code: 0,
        msg: 'success'
      }
    } else {
      this.ctx.body = {
        code: -1,
        msg: 'fail'
      }
    }
  }

  async serarchKey() {
    // select * from bookList where bookName like '%飞%';
    const { keywords } = this.ctx.query;
    const queryData = await this.ctx.app.mysql.query(`select * from bookList where bookName like '%${keywords}%';`)
    this.ctx.body = {
      code: 0,
      msg: 'success',
      data: queryData
    }
  }

  async register() {
    const { username, password } = this.ctx.request.body;
    const insertData = await this.ctx.app.mysql.insert('users', {username, password})
    this.ctx.body = {
      code: 0,
      msg: 'success',
      insertData
    }
  }

  /**
   * bookList 表：
   * 1. 增 --- postman
   * 2. 删 --- postman
   * 3. 改 --- postman
   *
   * 4. 查 --- 接口  /api/findBookList 分页 排序功能 order by  get
   *
   *    详情信息接口  /api/bookDetail/:bookId    get
  */

  async addBookList() {
    const { bookName, bookType } = this.ctx.request.body;
    const insertData = await this.ctx.app.mysql.insert('bookList', {
      bookName,
      bookType
    })
    this.ctx.body = {
      code: 0,
      msg: 'success',
      data: insertData
    }
  }

  async findBookList() {
    const {page, size, order = 'asc'} = this.ctx.query;
    const bookData = await this.ctx.app.mysql.select('bookList', {
      offset: page * Number(size),
      limit: size,
      orders: [['bookId', order]]
    })
    this.ctx.body = {
      code: 0,
      msg: 'success',
      data: bookData
    }
  }

  async findBookDetail() {
    const { bookId } = this.ctx.params;
    const bookData = await this.ctx.app.mysql.get('bookList', {bookId})
    this.ctx.body = {
      code: 0,
      msg: 'success',
      data: bookData
    }
  }
  /**
   * bookRack 书架： bookRackId userId bookId divideId
   * 1. 增  添加进入 书架 /api/addBookItem     post
   * 2. 删  删除 /api/deleteBookItem
   * 3. 改  分组  /api/updateDivideId
   * 4. 查  查分组 / 书架  /api/findBookRack   （分组信息）
   *
   */

  async findBookRack() {
    const {page, size, order = 'asc'} = this.ctx.query;
    const bookData = await this.ctx.app.mysql.select('bookRack', {
      offset: page * Number(size),
      limit: size,
      orders: [['bookRackId', order]]
    })
    this.ctx.body = {
      code: 0,
      msg: 'success',
      data: bookData
    }
  }

  async addBookItem() {
    const { bookId } = this.ctx.query;
    const { userId = 1 } = this.ctx.state; // 通过中间件获取到用户身份
    // 1. 先看书架中是否有这本书 如果有这本书，提示用户已经存在
    const getBookItem = await this.app.mysql.get('bookRack', { bookId })
    if (getBookItem) {
      this.ctx.body = {
        msg: '书籍已经在书架中了',
        code: -1,
      }
    } else {
      const insertBook = await this.app.mysql.insert('bookRack', { bookId, userId })
      this.ctx.body = {
        data: insertBook,
        code: 0,
        msg: 'success'
      }
    }
  }

  async deleteBookItem() {
    const { bookRackId } = this.ctx.params;
    const deleteData = await this.ctx.app.mysql.delete('bookRack', {bookRackId})
    this.ctx.body = {
      code: 0,
      msg: 'success',
      data: deleteData
    }
  }

  async updateDivideId() {
    const { bookRackId, divideId } = this.ctx.request.body;
    const updateData = await this.ctx.app.mysql.update('bookRack', {
      divideId
    }, {
      where: {
        bookRackId
      }
    })
    this.ctx.body = {
      code: 0,
      msg: 'success',
      data: updateData
    }
  }
}

module.exports = HomeController;
